public class MyQueue {
    static class Node{
        public int val;
        public Node prev;
        public Node next;

        public Node(int val) {
            this.val = val;
        }
    }

    /**
     * 规定队尾是链表的尾巴
     * 规定队头是链表的头
     */

    public Node front;//队头
    public Node rear;//队尾
    public void offer(int val){//入队列
        Node node = new Node(val);

        //第一个节点的插入
        if(front == null){
            front = node;
            rear = node;
        }else{
            rear.next = node;
            node.prev = rear;
            rear = node;
        }
    }

    public int size(){
        int count = 0;
        Node cur = front;
        while(cur != null){
            cur = cur.next;
            count++;
        }
        return count;
    }

    public int poll(){//出队列
        if(front == null){
            return -1;
        }
        int val = front.val;
        front = front.next;

        //防止只有一个节点
        if(front != null){
            front.prev = null;
        }
        return val;
    }

    public int peek(){//获取队头元素
        if(front == null){
            return -1;
        }
        return front.val;
    }
}